package com.xbongbong.saas.enums.dictionary;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.constant.PaasConstant;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 生产单 状态
 *
 * @author long.rao
 * @version v1.0
 * @date 2019/2/17 17:34
 * @since v1.0
 */
@Getter
public enum ProductionOrderStatusEnum {
    /**
     * UNFINISHED，表示未完成
     * FINISHED，表示已完成
     * CLOSED，表示已关闭
     */
    UNFINISHED(1, "unfinished", "enum.productionOrderStatus.alias.unfinished"),
    FINISHED(2, "finished", "enum.productionOrderStatus.alias.finished"),
    CLOSED(3, "closed", "enum.productionOrderStatus.alias.closed"),
    UNKNOW(404, "unkonw", "enum.productionOrderStatus.alias.unknow");

    private Integer code;
    private String attr;
    private String alias;

    ProductionOrderStatusEnum(Integer code, String attr, String alias) {
        this.code = code;
        this.attr = attr;
        this.alias = alias;
    }

    public static ProductionOrderStatusEnum getByCode(Integer code) {
        for (ProductionOrderStatusEnum statusEnum : values()) {
            if (Objects.equals(statusEnum.code, code)) {
                return statusEnum;
            }
        }
        return UNKNOW;
    }

    public static ProductionOrderStatusEnum getByAlias(String alias) {
        for (ProductionOrderStatusEnum statusEnum : values()) {
            if (Objects.equals(statusEnum.getAlias(), alias)) {
                return statusEnum;
            }
        }
        return UNKNOW;
    }

    /**
     * 获取全部枚举
     */
    public static List<ProductionOrderStatusEnum> getAllEnum() {
        List<ProductionOrderStatusEnum> list = new ArrayList<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        for (ProductionOrderStatusEnum cache : values()) {
            if (cache == UNKNOW) {
                continue;
            }
            list.add(cache);
        }
        return list;
    }

    public static List<JSONObject> getItemPoJoList(String status) {
        List<JSONObject> itemPoJoList = new ArrayList<>();
        List<ProductionOrderStatusEnum> allEnum = getAllEnum();
        allEnum.forEach(item -> {
            JSONObject itemObj = new JSONObject();
            itemObj.put("text", item.getAlias());
            itemObj.put("value", item.getCode());
            if (Objects.equals(Objects.toString(item.getCode()), status)) {
                itemObj.put("checked", true);
            }
            itemPoJoList.add(itemObj);
        });
        return itemPoJoList;
    }

    public String getAlias() {
        return I18nMessageUtil.getMessage(alias);
    }
}
